package com.example.controller;


import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.common.Result;
import com.example.entity.File;
import com.example.entity.Filetype;
import com.example.service.FileService;
import com.example.service.FiletypeService;
import com.example.service.LogService;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author admin
 * @since 2021-06-01
 */
@RestController
@RequestMapping("//filetype")
public class FiletypeController {
    public static final ConcurrentHashMap<String, Filetype> MAP = new ConcurrentHashMap<>();

    @Resource
    private FiletypeService filetypeService;
    @Resource
    private LogService logService;
    @Resource
    private HttpServletRequest request;

    @PostMapping
    public Result<?> save(@RequestBody Filetype filetype) {
        logService.log(StrUtil.format("新增文件类别：{} ",filetype.getFiletype()));
        return Result.success(filetypeService.save(filetype));
    }

    @PutMapping
    public Result<?> update(@RequestBody Filetype filetype) {
        logService.log(StrUtil.format("更新文件类别：{} ", filetype.getFiletype()));
        return Result.success(filetypeService.updateById(filetype));
    }

    @DeleteMapping("/{id}")
    public Result<?> delete(@PathVariable Long id) {
        Filetype filetype = filetypeService.getById(id);
        logService.log(StrUtil.format("删除文件类别：{} ",filetype.getFiletype()));
        filetypeService.removeById(id);
        return Result.success();
    }
    @GetMapping("/{id}")
    public Result<Filetype> findById(@PathVariable Long id) {
        return Result.success(filetypeService.getById(id));
    }

    @GetMapping
    public Result<List<Filetype>> findAll() {
        return Result.success(filetypeService.list());
    }
    @GetMapping("/page")
    public Result<IPage<Filetype>> findPage(@RequestParam(required = false, defaultValue = "") String name,
                                        @RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                        @RequestParam(required = false, defaultValue = "10") Integer pageSize) {
        return Result.success(filetypeService.page(new Page<>(pageNum, pageSize), Wrappers.<Filetype>lambdaQuery().like(Filetype::getFiletype, name)));
    }
}

